package com.hzlx.productionProducts.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.hzlx.*;
import com.hzlx.productionProducts.domain.ProductionProductsWithCounts;
import com.hzlx.productionProducts.domain.insertProductsWithCount;
import com.hzlx.productionProducts.feign.baseDataService;
import com.hzlx.productionProducts.feign.materialService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hzlx.productionProducts.mapper.ProductionProductsMapper;
import com.hzlx.productionProducts.domain.ProductionProducts;
import com.hzlx.productionProducts.service.IProductionProductsService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产产品Service业务层处理
 * 
 * @author dk
 * @date 2025-07-01
 */
@Service
public class ProductionProductsServiceImpl implements IProductionProductsService 
{
    @Autowired
    private ProductionProductsMapper productionProductsMapper;
@Resource
private baseDataService baseDataService;
@Resource
private materialService materialService;
    /**
     * 查询生产产品
     * 
     * @param productId 生产产品主键
     * @return 生产产品
     */
    @Override
    public ProductionProducts selectProductionProductsByProductId(Integer productId)
    {
        return productionProductsMapper.selectProductionProductsByProductId(productId);
    }

    /**
     * 查询生产产品列表
     * 
     * @param productionProducts 生产产品
     * @return 生产产品
     */
    @Override
    public List<ProductionProducts> selectProductionProductsList(ProductionProducts productionProducts)
    {
        return productionProductsMapper.selectProductionProductsList(productionProducts);
    }

    /**
     * 新增生产产品
     * 
     * @param productionProducts 生产产品
     * @return 结果
     */
    @Override
    public int insertProductionProducts(ProductionProducts productionProducts)
    {
        //1、获取产品id  根据产品id 查询bom表 获取bomId
        BomsDto bomsDto=new BomsDto();
        bomsDto.setProductId(Long.valueOf(productionProducts.getProductTypeId()));
        List<BomsDto> list=baseDataService.list(bomsDto);
        System.out.println("查到的bom---------------------");
        System.out.println(list);
        //2、根据bom_Id 和 operation_id查询mes_bom_components表,返回该道工序所需要的物料
        BomComponentsDto bomComponentsDto=new BomComponentsDto();
        bomComponentsDto.setBomId(list.get(0).getBomId());
        bomComponentsDto.setOperationId(1L);
     List<BomComponentsDto> BomComponentsDtoS=baseDataService.BomComponentsList(bomComponentsDto);
        System.out.println("查到的所需要消耗的物料");
        System.out.println(BomComponentsDtoS);
    for (BomComponentsDto bomComponents : BomComponentsDtoS){
        System.out.println("bomComponents；ID"+bomComponents.getComponentMaterialId()+"消耗了"+bomComponents.getQuantity());
        MesBatchesMaterialDto mesBatchesMaterialDto=new MesBatchesMaterialDto();
        mesBatchesMaterialDto.setCurrentQuantity(bomComponents.getQuantity());
        mesBatchesMaterialDto.setMaterialId(Long.valueOf(bomComponents.getComponentMaterialId()));
        materialService.editByOperation(mesBatchesMaterialDto);
    }
        //3、拿着获取的物料id去调用mes_batches_material 的数据进行更新
        return productionProductsMapper.insertProductionProducts(productionProducts);
    }

    /**
     * 修改生产产品
     * 
     * @param productionProducts 生产产品
     * @return 结果
     */
    @Override
    public int updateProductionProducts(ProductionProducts productionProducts)
    {
        //1、获取产品id  根据产品id 查询bom表 获取bomId
        BomsDto bomsDto=new BomsDto();
        bomsDto.setProductId(Long.valueOf(productionProducts.getProductTypeId()));
        List<BomsDto> list=baseDataService.list(bomsDto);
        System.out.println("查到的bom---------------------");
        System.out.println(list);
        //2、根据bom_Id 和 operation_id查询mes_bom_components表,返回该道工序所需要的物料
        BomComponentsDto bomComponentsDto=new BomComponentsDto();
        bomComponentsDto.setBomId(list.get(0).getBomId());
        bomComponentsDto.setOperationId((long)(productionProducts.getOperationId()-1));
        List<BomComponentsDto> BomComponentsDtoS=baseDataService.BomComponentsList(bomComponentsDto);
        System.out.println("查到的所需要消耗的物料");
        System.out.println(BomComponentsDtoS);
        for (BomComponentsDto bomComponents : BomComponentsDtoS){
            System.out.println("bomComponents；ID"+bomComponents.getComponentMaterialId()+"消耗了"+bomComponents.getQuantity());
            MesBatchesMaterialDto mesBatchesMaterialDto=new MesBatchesMaterialDto();
            mesBatchesMaterialDto.setCurrentQuantity(bomComponents.getQuantity());
            mesBatchesMaterialDto.setMaterialId(Long.valueOf(bomComponents.getComponentMaterialId()));
            materialService.editByOperation(mesBatchesMaterialDto);
        }
        return productionProductsMapper.updateProductionProducts(productionProducts);
    }

    /**
     * 批量删除生产产品
     * 
     * @param productIds 需要删除的生产产品主键
     * @return 结果
     */
    @Override
    public int deleteProductionProductsByProductIds(Integer[] productIds)
    {
        return productionProductsMapper.deleteProductionProductsByProductIds(productIds);
    }

    @Override
    public void deleteProductionProductsByDo(Long productId) {
             productionProductsMapper.deleteProductionProductsByDo(productId);
    }

    /**
     * 删除生产产品信息
     * 
     * @param productId 生产产品主键
     * @return 结果
     */
    @Override
    public int deleteProductionProductsByProductId(Integer productId)
    {
        return productionProductsMapper.deleteProductionProductsByProductId(productId);
    }

    @Override
    public ProductionProducts selectFirstProductByOperationId(Integer operationId) {
        return productionProductsMapper.selectFirstProductByOperationId(operationId);
    }

    @Override
    public void updateProductionProductsByFinished(ProductionProductsDto productionProductsDto) {
        ProductionProducts productionProducts=new ProductionProducts();
        productionProducts.setProductId(productionProductsDto.getProductId());
        productionProducts.setOperationId(0);
        productionProductsMapper.updateProductionProducts(productionProducts);

    }

    @Override
    public void insertProductionProductsByBadGoods(ProductionProductsDto productionProductsDto) {
        productionProductsMapper.insertProductionProductsByBadGoods(productionProductsDto);
    }

    @Override
    public ResultDto<String> updateProductionProductsByCounts(ProductionProductsWithCounts productionProductsWithCounts) {
        BomsDto bomsDto=new BomsDto();
        bomsDto.setProductId(Long.valueOf(productionProductsWithCounts.getProductTypeId()));
        List<BomsDto> list=baseDataService.list(bomsDto);
        System.out.println("查到的bom---------------------");
        System.out.println(list);
        //2、根据bom_Id 和 operation_id查询mes_bom_components表,返回该道工序所需要的物料
        BomComponentsDto bomComponentsDto=new BomComponentsDto();
        bomComponentsDto.setBomId(list.get(0).getBomId());
        bomComponentsDto.setOperationId((long)(productionProductsWithCounts.getOperationId()));
        List<BomComponentsDto> BomComponentsDtoS=baseDataService.BomComponentsList(bomComponentsDto);
        System.out.println("查到的所需要消耗的物料");
        System.out.println(BomComponentsDtoS);
        for (BomComponentsDto bomComponents : BomComponentsDtoS){
            System.out.println("bomComponents；ID"+bomComponents.getComponentMaterialId()+"消耗了"+bomComponents.getQuantity());
            MesBatchesMaterialDto mesBatchesMaterialDto=new MesBatchesMaterialDto();
            mesBatchesMaterialDto.setCurrentQuantity(bomComponents.getQuantity().multiply(BigDecimal.valueOf(productionProductsWithCounts.getCount())));
            mesBatchesMaterialDto.setMaterialId(Long.valueOf(bomComponents.getComponentMaterialId()));
            materialService.editByOperation(mesBatchesMaterialDto);
        }
        productionProductsMapper.updateProductionProductsByCounts(productionProductsWithCounts);
        return ResultDto.<String>builder().code(200).build();
    }

    @Override
    @GlobalTransactional
    public ResultDto<String> insertProductsWithCount(insertProductsWithCount insertProductsWithCount) {
        BomsDto bomsDto=new BomsDto();
        bomsDto.setProductId(Long.valueOf(insertProductsWithCount.getProductTypeId()));
        List<BomsDto> BomList=baseDataService.list(bomsDto);
        System.out.println("查到的bom---------------------");
        System.out.println(BomList);
        //2、根据bom_Id 和 operation_id查询mes_bom_components表,返回该道工序所需要的物料
        BomComponentsDto bomComponentsDto=new BomComponentsDto();
        bomComponentsDto.setBomId(BomList.get(0).getBomId());
        bomComponentsDto.setOperationId(1L);
        List<BomComponentsDto> BomComponentsDtoS=baseDataService.BomComponentsList(bomComponentsDto);
        System.out.println("查到的所需要消耗的物料");
        System.out.println(BomComponentsDtoS);
        for (BomComponentsDto bomComponents : BomComponentsDtoS){
            System.out.println("bomComponents；ID"+bomComponents.getComponentMaterialId()+"消耗了"+bomComponents.getQuantity());
            MesBatchesMaterialDto mesBatchesMaterialDto=new MesBatchesMaterialDto();
            mesBatchesMaterialDto.setCurrentQuantity(bomComponents.getQuantity().multiply(BigDecimal.valueOf(insertProductsWithCount.getCount())));
            mesBatchesMaterialDto.setMaterialId(Long.valueOf(bomComponents.getComponentMaterialId()));

                // 循环调用 material-service
            AjaxResult result =  materialService.editByOperation(mesBatchesMaterialDto);
            if (result == null || !"200".equals(String.valueOf(result.get("code")))) {
                String msg = result != null ? String.valueOf(result.get("msg")) : "返回为空";
                throw new RuntimeException("调用物料服务失败：" + msg);
            }

        }
        List<ProductionProducts> list=new ArrayList<>();
        for (int i=0;i<insertProductsWithCount.getCount();i++){
            ProductionProducts productionProducts=new ProductionProducts();
            productionProducts.setProductName(insertProductsWithCount.getProductName());
            productionProducts.setOperationId(insertProductsWithCount.getOperationId());
            productionProducts.setUnitOfMeasure(insertProductsWithCount.getUnitOfMeasure());
            productionProducts.setCreatedAt(insertProductsWithCount.getCreatedAt());
            productionProducts.setProductTypeId(insertProductsWithCount.getProductTypeId());
            list.add(productionProducts);
        }
        productionProductsMapper.insertProductsByCount(list);
        return ResultDto.<String>builder().code(200).build();
    }
}
